package uk.co.richardwkirk.parkrun.funnels.model;

import static org.junit.Assert.*;

import org.junit.Test;

import uk.co.richardwkirk.parkrun.funnels.model.RaceFinishers;
import uk.co.richardwkirk.parkrun.funnels.model.Runner;


public class RaceFinishersLoad {

	protected void loadAndCheckTestFile(String resource)
	{
		// Load the resource file and check the contents is what we expect
		RaceFinishers finishers = null;
		try 
		{
			finishers = RaceFinishers.createFromResource(resource);
		}
		catch (Exception e) 
		{
			e.printStackTrace(System.err);
			fail("Exception thrown during test.");
		}
		
		// Check that the finishers returned are what we expect
		checkLoadedFinishers(finishers);
	}
	
	protected void checkLoadedFinishers(RaceFinishers finishers)
	{
		compareRunner("First finisher", new Runner("Adam CLARKE", 1, 911000), finishers.getFinisher(1));
		compareRunner("Last finisher", new Runner("Sally WILLIS", 914, 3316000), finishers.getFinisher(914));
		compareRunner("Richard Kirk", new Runner("Richard William KIRK", 709, 1757000), finishers.getFinisher(709));
		assertEquals("Loaded the correct number of finishers.", finishers.size(), 914);	
	}
	
	private void compareRunner(String identifier, Runner expected, Runner actual) {
		assertNotNull("Runner not found.", actual);
		assertEquals("Checking " + identifier + " name", expected.getName(), actual.getName());
		assertEquals("Checking " + identifier + " name", expected.getPosition(), actual.getPosition());
		assertEquals("Checking " + identifier + " name", expected.getFinishTime(), actual.getFinishTime());
	}

	@Test
	public void testParseCSV() 
	{
		loadAndCheckTestFile("classpath:/Bushy-2012-01-28.csv");
	}

	@Test
	public void testParseHTML() 
	{
		loadAndCheckTestFile("classpath:/Bushy-2012-01-28.html");
	}

}
